package dparish.client;

import javax.inject.Inject;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.Window;
import com.google.inject.Provider;

import dparish.client.activity.StudentActivity;
import dparish.client.activity.StudentListActivity;
import dparish.client.places.StudentAddEditPlace;
import dparish.client.places.StudentListPlace;
import dparish.client.proxy.StudentProxy;
import dparish.presenters.StudentListPresenterTest;

public class AppActivityMapper implements ActivityMapper {

	// I know, you aren't supposed to do field level injection but it makes sense here
	// to me.  You can still test this all you want since the module determines what the 
	// actual view is and this keeps you from having gobbs of constructor arguments
	// if you have alot of activities.
	@Inject StudentListActivity studentListActivity;
	@Inject StudentActivity studentActivity;
	
	@Override
	public Activity getActivity(Place place) {
		if (place instanceof StudentListPlace) {
			return studentListActivity;
		}
		if (place instanceof StudentAddEditPlace) {
			StudentAddEditPlace studentAddEditPlace = (StudentAddEditPlace) place;
			StudentProxy student = studentAddEditPlace.getStudent();

			// If the student is null but we have an id in the url, than this is a bookmarked page so we need
			// to create an empty student and set the id so it can be looked up in the activity.
			if (student == null && (!studentAddEditPlace.getUniqueId().isEmpty())) {
				student = studentActivity.createEmptyStudent();
				student.setId(Long.parseLong(studentAddEditPlace.getUniqueId()));
			}
			studentActivity.setEntity(student);
			return studentActivity;
		}
		return null;
	}

}
